package com.java.base.exercise.json;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author RenPu
 * @Date 2021/10/22 18:22
 * @Version 1.0
 * @Description:
 **/
public class TestJson {


    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    static class Database {
        private Integer id;
        private String firstData;
        private String levelData;


    }

    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    static class Model {
        private Integer id;
        private List<Item> firstData;
        private List<Item> levelData;
    }

    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    static class Item {
        private String value;
        private String name;

    }

    public static void main(String[] args) {
        List<Item> list1 = new ArrayList<>();
        list1.add(Item.builder().name("第一段一列").value("值").build());
        List<Item> list2 = new ArrayList<>();
        list2.add(Item.builder().name("第一段一列").value("值").build());
        Model model = Model.builder().id(1).firstData(list1).levelData(list2).build();


        //新增方法
        //下面这个存数据库
        Database build = Database.builder().id(1).firstData(JSON.toJSONString(model.getFirstData())).levelData(JSON.toJSONString(model.getLevelData())).build();

        System.out.println("准备放入数据库的值；......" + build);


        //查询方法
        //从数据库里面取出来的处理
        Model m = Model.builder().id(1).firstData(JSON.parseArray(build.getFirstData(), Item.class)).levelData(JSON.parseArray(build.getLevelData(), Item.class)).build();

        System.out.println("从数据库获取的数据：......." + m);
    }
}
